﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

using VhaBot.Common;
using VhaBot.Net;
using VhaBot.Net.Events;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Net;
using System.Diagnostics;

namespace AoCchatUI
{
    public partial class ParentForm : Form
    {       
        AocBotDS botDS;

        private static string _owner;
        public static string Owner { get { return _owner; } }

        public Chat chat;

        ArrayList channels = new ArrayList();

        Dictionary<string, ChildChannelForm> childForms = new Dictionary<string, ChildChannelForm>();

        public ParentForm()
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
        }
       

        private void MDIParent1_Load(object sender, EventArgs e)
        {

            ChildDebugForm debugForm = new ChildDebugForm();
            debugForm.MdiParent = this;
            debugForm.Text = "Debug";
            debugForm.Show();
                       
            botDS = new AocBotDS();

            if (File.Exists("botDS.xml"))
            {
                botDS.ReadXml("botDS.xml");
            }
            else
            {
                botDS.WriteXml("botDS.xml");
            }                    

            // Read configuration      
            Configuration configuration = Configuration.Read("Config.xml");
            if (configuration == null)
            {
                //rtbConsole.Text += "Unable to read configuration\n";
                return;
            }
            _owner = configuration.Owner;
            
            // Initialize chat connection
            chat = new Chat(configuration.Server, configuration.Port, configuration.Username, configuration.Password, configuration.Character);

            //chat.TellEvent += new TellEventHandler(OnTellEvent);
            //chat.ChannelMessageEvent += new ChannelMessageEventHandler(OnChannelMessageEvent);
            //chat.ChannelMessageEvent += new ChannelMessageEventHandler(OnChannelRegionEvent);
            //chat.FriendStatusEvent += new FriendStatusEventHandler(OnFriendStatusEvent);
            //chat.PrivateMessageEvent += new PrivateMessageEventHandler(OnPrivateMessageEvent);

            
            ChildChannelForm channelFormGlobal = new ChildChannelForm(chat, "~Global");
            channelFormGlobal.MdiParent = this;
            channelFormGlobal.Text = "Global";
            channelFormGlobal.Show();

            ChildChannelForm channelFormGuild = new ChildChannelForm(chat, "~Guild");
            channelFormGuild.MdiParent = this;
            channelFormGuild.Text = "Guild";
            channelFormGuild.Show();

            //chat.ChannelJoinEvent += new  ChannelJoinEventHandler(OnChannelJoinEvent);

            chat.Connect();
            
            /*
            foreach (string channel in channels)
            {
                //childForms.     Add(new ChildChannelForm(chat, channel));
                childForms.Add(channel, new ChildChannelForm(chat, channel));

                //ChildChannelForm channelForm = new ChildChannelForm(chat, channel);
                childForms[channel].MdiParent = this;
                childForms[channel].Text = channel;
                childForms[channel].Show();
            }
             */
        }

        private void OnChannelJoinEvent(Chat chat, ChannelJoinEventArgs e)
        {
            string channel = e.Name;

            if (channel.Substring(0, 1) == "~" && channels.Count == 0)
            {
                channels.Add(channel);
            }
        }

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        private void ShowNewForm(object sender, EventArgs e)
        {

        }
    }
}
